#include "mainwindow.h"

#include <QStyleFactory>
#include <QApplication>
#include <QToolBar>

#include "qtcpp/common.h"

MainWindow::MainWindow(QWidget* parent /*= nullptr*/)
	:QMainWindow(parent),
	m_pPropertyView(nullptr),
	m_pOgreWindow(nullptr),
	m_pHSplitter(nullptr),
	m_pActShow3DRoi(nullptr),
	m_pActShowGrid(nullptr),
	m_pActShowAxes(nullptr)
{
	initStyle();
	initToolbar();
	initLayout();
}

MainWindow::~MainWindow()
{

}

void MainWindow::showEvent(QShowEvent* event)
{
	m_pHSplitter->setSizes({ 300,800 });

	QMainWindow::showEvent(event);
}

void MainWindow::initToolbar()
{
	auto tbState = addToolBar("State");
	m_pActShowGrid = tbState->addAction(QIcon(":/images/grid.png"), "grid");
	m_pActShowGrid->setCheckable(true);
	m_pActShowGrid->setChecked(true);
	m_pActShowAxes = tbState->addAction(QIcon(":/images/axes.png"), "axes");
	m_pActShowAxes->setCheckable(true);
	m_pActShowAxes->setChecked(true);
	m_pActShow3DRoi = tbState->addAction(QIcon(":/images/roi.png"), "3droi");
	m_pActShow3DRoi->setCheckable(true);

	connect(m_pActShow3DRoi, &QAction::triggered, [=]() {slotToolBar(m_pActShow3DRoi); });
	connect(m_pActShowGrid, &QAction::triggered, [=]() {slotToolBar(m_pActShowGrid); });
	connect(m_pActShowAxes, &QAction::triggered, [=]() {slotToolBar(m_pActShowAxes); });
}

void MainWindow::initLayout()
{
	auto central = new QWidget;
	setCentralWidget(central);

	auto mainLayout = new QHBoxLayout(central);
	mainLayout->setContentsMargins(0, 0, 0, 0);

	m_pHSplitter = new QSplitter;
	m_pPropertyView = new EasyLib::PropertyView;
	m_pOgreWindow = new OgreWindow;
	m_pOgreWindow->setBackgroundColor(QColor("#2d2d2d"));
	m_pHSplitter->addWidget(m_pPropertyView);
	m_pHSplitter->addWidget(m_pOgreWindow);

	mainLayout->addWidget(m_pHSplitter);

	connect(m_pOgreWindow, &OgreWindow::signal_propertyInfo, m_pPropertyView, &EasyLib::PropertyView::setup);
	connect(m_pPropertyView, &EasyLib::PropertyView::signal_propertyInfo, m_pOgreWindow, &OgreWindow::setupProperty);
}

void MainWindow::initStyle()
{
	qApp->setStyle(QStyleFactory::create("Fusion"));
	qApp->setStyleSheet(EasyLib::QtCpp::getFileContent(":/qss/common.qss"));

	auto currentPalette = qApp->palette();
	currentPalette.setColor(QPalette::AlternateBase, QColor("#202020"));
	qApp->setPalette(currentPalette);
}

void MainWindow::slotToolBar(QAction* act)
{
	if (act == m_pActShow3DRoi)
	{
		m_pOgreWindow->showShape(m_pActShow3DRoi->isChecked(),m_pOgreWindow->getShape(Em_Roi3D));
	}
	else if (act == m_pActShowGrid)
	{
		m_pOgreWindow->showShape(m_pActShowGrid->isChecked(), m_pOgreWindow->getShape(Em_Grid));
	}
	else if (act == m_pActShowAxes)
	{
		m_pOgreWindow->showShape(m_pActShowAxes->isChecked(), m_pOgreWindow->getShape(Em_Axes));
	}
}

